package com.itcast.donkey.adapters;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;


import com.itcast.donkey.R;
import com.itcast.donkey.modles.InFigureModels;
import com.squareup.picasso.Picasso;

/**
 * Created by lanouhn on 16/9/1.
 */
public class InFigureAdapter extends RecyclerView.Adapter<InFigureAdapter.InFigureViewHolder> {
    private Context context;
    private InFigureModels inFigureModels;
    private int count;

    public InFigureAdapter(Context context, InFigureModels inFigureModels) {
        this.context = context;
        this.inFigureModels = inFigureModels;
        this.count = inFigureModels.getArticle_sections().size() + 1;
    }

    @Override
    public InFigureViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_in_figure, null);
        InFigureViewHolder holder = new InFigureViewHolder(view);
        return holder;
    }

    @Override
    public int getItemViewType(int position) {
        return position;
    }

    @Override
    public void onBindViewHolder(InFigureViewHolder holder, int position) {
        if (position == 0) {
            holder.name.setText(inFigureModels.getName());
            holder.name.setVisibility(View.VISIBLE);
            holder.iv_line.setVisibility(View.VISIBLE);
            holder.title.setText(inFigureModels.getTitle());
            holder.title.setVisibility(View.VISIBLE);
//            holder.description.setText(inFigureModels.getArticle_sections().get(0).getDescription());
//            holder.description.setVisibility(View.VISIBLE);
            Picasso.with(context).load(inFigureModels.getImage_url()).into(holder.imageUrl);
            holder.imageUrl.setVisibility(View.VISIBLE);
        } else {
            int i = position - 1;
            if (!"".equals(inFigureModels.getArticle_sections().get(i).getTitle())) {
                holder.itemTitle.setText(inFigureModels.getArticle_sections().get(i).getTitle());
                holder.itemTitle.setVisibility(View.VISIBLE);
            }
            if (!"".equals(inFigureModels.getArticle_sections().get(i).getDescription())) {
                holder.itemDescription.setText(inFigureModels.getArticle_sections().get(i).getDescription());
                holder.itemDescription.setVisibility(View.VISIBLE);
                holder.item_line.setVisibility(View.VISIBLE);
            }
            if (!"".equals(inFigureModels.getArticle_sections().get(i).getImage_url())) {
                Picasso.with(context).load(inFigureModels.getArticle_sections().get(i).getImage_url()).into(holder.itemImageUrl);
                holder.itemImageUrl.setVisibility(View.VISIBLE);
            }
        }
    }

    @Override
    public int getItemCount() {
        return count;
    }


    public class InFigureViewHolder extends RecyclerView.ViewHolder {
        private ImageView imageUrl;
        private TextView name;
        private ImageView iv_line;
        private TextView title;
        private TextView description;
        private TextView itemTitle;
        private TextView itemDescription;
        private View item_line;
        private ImageView itemImageUrl;


        public InFigureViewHolder(View itemView) {
            super(itemView);
            imageUrl = (ImageView) itemView.findViewById(R.id.image_url);
            name = (TextView) itemView.findViewById(R.id.name);
            iv_line = (ImageView) itemView.findViewById(R.id.iv_line);
            title = (TextView) itemView.findViewById(R.id.title);
            description = (TextView) itemView.findViewById(R.id.description);

            itemTitle = (TextView) itemView.findViewById(R.id.item_title);
            itemDescription = (TextView) itemView.findViewById(R.id.item_description);
            item_line = itemView.findViewById(R.id.item_line);
            itemImageUrl = (ImageView) itemView.findViewById(R.id.item_image_url);
        }
    }
}
